package com.android.projectsquirrel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter_Survey2
{
	public static final String KEY_ROWID = "_id";
    public static final String KEY_PROFILENAME = "profileName";
    public static final String KEY_DAY = "day";
    public static final String KEY_MONTH = "month";
    public static final String KEY_YEAR = "year";
    public static final String KEY_HOUR = "hour";
    public static final String KEY_MINUTE = "minute";
    public static final String KEY_AMPM = "ampm";   
    public static final String KEY_EMAIL = "email"; 
    public static final String KEY_GRAYSQUIRRELS = "graysquirrels"; 
    public static final String KEY_FOXSQUIRRELS = "foxsquirrels"; 
    public static final String KEY_FOODSOURCE = "foodsource"; 
    public static final String KEY_OTHERFOODSOURCES = "otherfoodsources";
    public static final String KEY_SQUIRRELCHANGE = "squirrelchange";
    public static final String KEY_DIFFERENTSITE = "differentsite";
    public static final String KEY_COMMENTS= "comments";
    private static final String TAG = "DBAdapter2";
    
    private static final String DATABASE_NAME = "DemosquirrelDatabase";
    private static final String DATABASE_TABLE = "demonewSquirrelTable";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table "+ DATABASE_TABLE+" (profileName text primary key,"
        + "day text not null, month text not null,year text not null,hour text not null,minute text not null, ampm text not null," 
        +"email text not null,graysquirrels text not null,foxsquirrels text not null,"
        + "foodsource integer not null,otherfoodsources text,squirrelchange integer not null,differentsite integer not null,comments text);";
        
    private final Context context; 
    
    private DatabaseHelper_2 DBHelper;
    private SQLiteDatabase db;

    public DBAdapter_Survey2(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper_2(context);
    }
        
    private static class DatabaseHelper_2 extends SQLiteOpenHelper 
    {
        DatabaseHelper_2(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
            onCreate(db);
        }
    }    
    
    //---opens the database---
    public DBAdapter_Survey2 open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    
    //---insert a title into the database---
    public long insertTitle2(String profilename,String day,String month, String year,String hour, String minute,String ampm,String email,String graySquirrels,String foxSquirrels,
    		int foodsource,String otherfoodsources,int squirrelchange,int differentSite,String comments) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PROFILENAME, profilename);
        initialValues.put(KEY_DAY, day);
        initialValues.put(KEY_MONTH,month);
        initialValues.put(KEY_YEAR, year);
        initialValues.put(KEY_HOUR, hour);
        initialValues.put(KEY_MINUTE, minute);
        initialValues.put(KEY_AMPM, ampm);
        initialValues.put(KEY_EMAIL, email);
        initialValues.put(KEY_GRAYSQUIRRELS, graySquirrels);
        initialValues.put(KEY_FOXSQUIRRELS, foxSquirrels);
        initialValues.put(KEY_FOODSOURCE, foodsource);
        initialValues.put(KEY_OTHERFOODSOURCES,otherfoodsources);
        initialValues.put(KEY_SQUIRRELCHANGE, squirrelchange);
        initialValues.put(KEY_DIFFERENTSITE, differentSite);
        initialValues.put(KEY_COMMENTS, comments);
        
        long value =  db.insert(DATABASE_TABLE, null, initialValues);
//        db.close();
        return value;
    }


    //---deletes a particular title---
//    public boolean deleteTitle(long rowId) 
//    {
//        return db.delete(DATABASE_TABLE, KEY_ROWID + 
//        		"=" + rowId, null) > 0;
//    }

    //---retrieves all the titles---
    
    //---retrieves a particular title---
    public Cursor getTitle2(String profileName) throws SQLException 
    {
        Cursor mCursor =
                db.query(true,DATABASE_TABLE, new String[] {
                		KEY_PROFILENAME,
                        KEY_DAY,
                        KEY_MONTH,
                        KEY_YEAR, 
                        KEY_HOUR,
                        KEY_MINUTE,
                        KEY_AMPM,
                        KEY_EMAIL, 
                        KEY_GRAYSQUIRRELS,
                        KEY_FOXSQUIRRELS,
                        KEY_FOODSOURCE,
                        KEY_OTHERFOODSOURCES,
                        KEY_SQUIRRELCHANGE, 
                        KEY_DIFFERENTSITE, 
                        KEY_COMMENTS,
                		}, 
                		KEY_PROFILENAME+"=?",new String[]{profileName},
                		null,
                		null, 
                		null, 
                		null 
                		);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    
    public Boolean checkProfile(String profileName){
    	Boolean recordExists = false;
    	Cursor cursor = db.query("demonewSquirrelTable",new String[]{"profileName"},
				 "profileName like \'"+profileName+"\'",null,null,null,null);
    	if(cursor.getCount()!=0){
    		recordExists = true;
    	}
    	return recordExists;
	}


//    //---updates a title---
    public boolean updateTitle2(String profilename,String day,String month, String year,String hour, String minute,String ampm,String email,String graySquirrels,String foxSquirrels,
    		int foodsource,String otherfoodsources,int squirrelchange,int differentSite,String comments) 
     
    {
        ContentValues args = new ContentValues();
        args.put(KEY_DAY, day);
        args.put(KEY_MONTH, month);
        args.put(KEY_YEAR, year);
        args.put(KEY_HOUR, hour);
        args.put(KEY_MINUTE, minute);
        args.put(KEY_AMPM, ampm);
        args.put(KEY_FOODSOURCE, foodsource);
        args.put(KEY_OTHERFOODSOURCES, otherfoodsources);
        args.put(KEY_SQUIRRELCHANGE, squirrelchange);
        args.put(KEY_DIFFERENTSITE, differentSite);
        args.put(KEY_COMMENTS, comments);
        return db.update(DATABASE_TABLE, args, 
        		KEY_PROFILENAME+"=?",new String[]{profilename})>0;
    }
}
